define(function (require, exports, module) {
    window._ = require('../lib/underscore');
    window.Backbone = require('../lib/backbone');       
    var Config = require('./Config');
    var MainView = require("./PDFView/MainView")
    var ReaderApp = require("./ReaderApp")
    var ImageDocumentLoader = require("./ImageEngine/DocumentLoader");
    var ImagePageViewRender = require("./ImageEngine/ImagePageViewRender");    
   
    window._.templateSettings = {
        interpolate: /\{\{(.+?)\}\}/g
    };
    var Viewer = function(viewerContainerId, options){     
         
        this.config = new Config(options);           
         
       // try {       
            switch (this.config.readerType) {
                case WebPDF.ReaderEngineType.IMAGE:
                {
                   this.documentLoader = new ImageDocumentLoader();
                   this.pageViewRender = new ImagePageViewRender();                         
                }     
                break;              
                case WebPDF.ReaderEngineType.HTML5:
                {
                    $.error("Unsupported engine.");                   
                }
                break; 
                default:
                {
                    $.error("Invalid type of reader.");
                }
            }  
          
            this.readerApp = new ReaderApp(this.config, viewerContainerId, this.documentLoader, this.pageViewRender);
         
            if (this.config.xAuthToken) {
            	this.readerApp.setSessionId(this.config.xAuthToken);
            }
        
            var op =  {
                getAsync: false,
                lng: options.language,
                load: "current",
                useCookie: false,
                useDataAttrOptions: true,
                defaultValueFromContent: false,
                fallbackLng: false,
                useLocalStorage: false, /* set true for production*/
                localStorageExpirationTime: 86400000, // in ms, default 1 week
                resGetPath:  '/__ns__-__lng__.json?' + new Date().getTime()
            };
            i18n.init(op, _.bind(function(t) {
              $("body").i18n();
            }, this));
     //   }
       // catch (ex) {
           // console.error(ex);
           // return null;
      // }                
     };
    
   _.extend(Viewer.prototype, {
        load : function () {
           // try {
               this.readerApp.initInstance();                       
           // }
           // catch (ex) {
            //    console.error(ex);
           // }
        },
       addIgnoreMouseEventClass : function(ignoreClass){
           this.readerApp.addIgnoreMouseEventClass(ignoreClass);
       },

        /**
         * Set tool by name.
         * @param toolName Name of tool to set.
         */
        setCurrentToolByName : function(toolName){
            this.readerApp.setCurrentToolByName(toolName);
        },

        /**
         * @description Returns the current tool handler.
         * @return {WebPDF.Tools.IReaderToolHandler} The current tool handler.
         */
        getCurToolHandler : function(){
            return this.readerApp.getCurToolHandler();
        },

        /**
         * @description Get tool handler by name.
         * @param {String} toolName Name of the tool.
         * @return {WebPDF.Tools.IReaderToolHandler} A instance of tool handler.
         */
        getToolHandlerByName : function(toolName){
            this.readerApp.getToolHandlerByName(toolName);
        },

        /**
         * @description Set current tool.
         * @param {WebPDF.Tools.IReaderToolHandler} tool The tool handler to set.
         */
        setCurToolHandler : function(tool){
            return this.readerApp.setCurToolHandler(tool);
        },

        /**
         * @description Get a plug-in instance by the name of the plug-in.
         * @param {String} pluginName The name of the plug-in.
         * @return {WebPDF.IReaderPlugin} The plug-in instance.
         */
        getPluginByName : function (pluginName) {
            return this.readerApp.getPluginByName(pluginName);
        }
   });
    
    _.extend(Viewer.prototype, Backbone.Events);
    
    exports.createViewer = function(viewerContainerId, options){
        var viewer = new Viewer(viewerContainerId, options);
        WebPDF.ViewerInstance = viewer;
        return viewer;
    };
})